package cn.bugstack.xfg.dev.test;

import com.alibaba.fastjson.JSONObject;
import com.alipay.api.AlipayApiException;
import com.alipay.api.AlipayClient;
import com.alipay.api.DefaultAlipayClient;
import com.alipay.api.domain.AlipayTradeQueryModel;
import com.alipay.api.domain.AlipayTradeRefundModel;
import com.alipay.api.request.AlipayTradePagePayRequest;
import com.alipay.api.request.AlipayTradeQueryRequest;
import com.alipay.api.request.AlipayTradeRefundRequest;
import com.alipay.api.response.AlipayTradeRefundResponse;
import lombok.extern.slf4j.Slf4j;
import org.junit.Before;
import org.junit.Test;

import java.math.BigDecimal;

@Slf4j
public class AliPayTest {

    // 「沙箱环境」应用ID - 您的APPID，收款账号既是你的APPID对应支付宝账号。获取地址；https://open.alipay.com/develop/sandbox/app
    public static String app_id = "2021000147676426";
    // 「沙箱环境」商户私钥，你的PKCS8格式RSA2私钥 -【秘钥工具】所创建的公户私钥
    public static String merchant_private_key = "MIIEvAIBADANBgkqhkiG9w0BAQEFAASCBKYwggSiAgEAAoIBAQDUvIpYGPHEv+FyOyOX36sggHiX8tFvTbuitDnv0t0SGcYMyqoNvvRhIxtJFarx3bl9IbEe+nCW5FQd2DUNB9zVfiytegh4dMyIa9jKHf+dWuiPAyfPTIWEjnkB+CACJAlgt9KolewBjtrJoTVgw0QgFLlGUCoB7SvBhO9n6Du/ly7QSDuwVMXwijNlHg+opLXXE3eCihItFIu1aSA7FlQximqMXufe3HlQOoBWkxW7T9yY89Hbwl74v9WfX5/0at+Vm1sLpc29vgeLChuBYeTrBZtj+vcjkyEHtrqt7vEwmm32jiG7gk8AZbfSqP0umIdt+J2H0Fc9HUeGZOeIv5ORAgMBAAECggEAX8FZ2BuqP3BLDvrVo5fW1DNflK2EXcA0dCEBbxSP04TsY3eZocRRBaQitPvezS2LP2q56gK6SQEPZ7uY7XubiDFZ9N/YDjgslD9O/nMQSojP4wd9lM6Rzr++cgd4iN4lpnY3iRCqgW0Gx/kbKKnb8UDCwIZ20hUqO9oVVMTwl+SnHT5pCGB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提供给你的支付宝公钥
    public static String alipay_public_key = "MIIBIjANBgkqhkiG9w0BAQEFAAOCAQ8AMIIBCgKCAQEAjT7Uzlo10IfQcC2m068lu86z9o+QI9lwbG9LAKul5bbXTFwifmJNGEa3HshgAY2ONtkoczQS05Ub0rIDqZTHifJAV8NMYbrTyWe/a3Y7lRZLTK3saKvs/NF+D25HEJA5QqM571TDnqdOHu16kSnQCLnsgnfIZ++TlOQUuttpuAGBcPdeaLyomIXBpbdfNwlPtIwxP7iPXKZb80lCvWYdbT6UqxCFGX2npQrh03lE+h433lWWpknBpRFGrWdkvyQ6L1ByigBSmWTDWy9Ocj1+Z6aKAy2K+QPmJP8e3CuBp7cpP9ytn4lg5RXO4yvC3ThmoqD34B5xFylZHvKSV5SSxwIDAQAB";
    // 「沙箱环境」服务器异步通知回调地址
    public static String notify_url = "http://syh.natapp1.cc/api/v1/alipay/alipay_notify_url";
    // 「沙箱环境」页面跳转同步通知页面路径 需http://格式的完整路径，不能加?id=123这类自定义参数，必须外网可以正常访问
    public static String return_url = "https://gaga.plus";
    // 「沙箱环境」
    public static String gatewayUrl = "https://openapi-sandbox.dl.alipaydev.com/gateway.do";
    // 签名方式
    public static String sign_type = "RSA2";
    // 字符编码格式
    public static String charset = "utf-8";

    private AlipayClient alipayClient;

    @Before
    public void init() {
        this.alipayClient = new DefaultAlipayClient(gatewayUrl,
                app_id,
                merchant_private_key,
                "json",
                charset,
                alipay_public_key,
                sign_type);
    }

    @Test
    public void test_aliPay_pageExecute() throws AlipayApiException {
        AlipayTradePagePayRequest request = new AlipayTradePagePayRequest();  // 发送请求的 Request类
        request.setNotifyUrl(notify_url);
        request.setReturnUrl(return_url);

        JSONObject bizContent = new JSONObject();
        bizContent.put("out_trade_no", "xfg2024092709120005");  // 我们自己生成的订单编号
        bizContent.put("total_amount", "0.01"); // 订单的总金额
        bizContent.put("subject", "测试商品");   // 支付的名称
        bizContent.put("product_code", "FAST_INSTANT_TRADE_PAY");  // 固定配置
        request.setBizContent(bizContent.toString());

        String form = alipayClient.pageExecute(request).getBody();
        log.info("测试结果\r\n\n把我复制到 index.html ->：{}", form);

    }

    /**
     * 查询订单
     */
    @Test
    public void test_alipay_certificateExecute() throws AlipayApiException {

        AlipayTradeQueryModel bizModel = new AlipayTradeQueryModel();
        bizModel.setOutTradeNo("xfg2024092709120005");

        AlipayTradeQueryRequest request = new AlipayTradeQueryRequest();
        request.setBizModel(bizModel);

        String body = alipayClient.execute(request).getBody();
        log.info("测试结果：{}", body);
    }

    /**
     * 退款接口
     */
    @Test
    public void test_alipay_refund() throws AlipayApiException {
        AlipayTradeRefundRequest request =new AlipayTradeRefundRequest();
        AlipayTradeRefundModel refundModel =new AlipayTradeRefundModel();
        refundModel.setOutTradeNo("daniel82AAAA000032333361X03");
        refundModel.setRefundAmount("1.00");
        refundModel.setRefundReason("退款说明");
        request.setBizModel(refundModel);

        AlipayTradeRefundResponse execute = alipayClient.execute(request);
        log.info("测试结果：{}", execute.isSuccess());
    }

}